trigger SyncNationalAndLocalActivityToCallPlan on ConsumerEducation__c (after update,after delete) {
     if(system.trigger.isupdate){
        list<id> CSEducationId=new list<id>();
        set<Date> SortDate=new set<date>();
        set<id> ownerId=new set<id>();
        Map<id,date> CSEducationDateMap=new map<id,date>();
        for(ConsumerEducation__c CSedu:trigger.new){
            if(trigger.oldmap.get(CSedu.id).date__c != CSedu.date__c && CSedu.Type__c!='PClass'){
                CSeducationid.add(CSedu.id);
                CSEducationDateMap.put(CSedu.id,CSedu.date__c);
                sortdate.add(CSedu.date__c);
                sortdate.add(trigger.oldmap.get(CSedu.id).date__c);
            }
        }
        
        
        
        list<callplan__c> CallPlanList=new list<callplan__c>();
        if(CSeducationid.size()>0){
            callplanlist=[select id,ActivityId__c from callplan__c where ActivityId__c in: CSeducationId];
        }
        for(CallPlan__c cp:callplanlist){
            cp.CallPlanDate__c=CSeducationDateMap.get(cp.activityid__c);
            cp.calldate__c=datetime.newinstance(CSeducationDateMap.get(cp.activityid__c),time.newInstance(9,0,0,0));
        }
        update callplanlist;
        
        list<event> eventlist=new list<event>();
        if(CSeducationid.size()>0){
            eventlist=[select id,whatid,ownerid from event where whatid in: CSeducationId];
        }
        for(event e:eventlist){
            e.StartDateTime=datetime.newinstance(CSeducationDateMap.get(e.whatid),time.newInstance(9,0,0,0));
            e.eventdate__c=CSeducationDateMap.get(e.whatid);
            ownerId.add(e.ownerid);
        }
        update eventlist;
        if(CSeducationid.size()>0){
            CalendarSorterNotFuture.sort(sortDate,Ownerid);
        }
    }
    if(system.trigger.isdelete){
        list<id> CSeducationId=new list<id>();
        set<Date> SortDate=new set<date>();
        set<id> ownerId=new set<id>();
        for(ConsumerEducation__c CSedu:trigger.old){
            if(CSedu.type__c!='PClass'){
                CSeducationid.add(CSedu.id);
                sortdate.add(csedu.date__c);
            }
        }
        list<callplan__c> CallPlanList=new list<callplan__c>();
        list<event> eventlist=new list<event>();
        if(CSeducationid.size()>0){
            callplanlist=[select id from callplan__c where ActivityId__c in: CSeducationId];
            eventlist=[select id,ownerid from event where whatid in: CSeducationId];
        }
        delete callplanlist;
        for(event e:eventlist){
            ownerid.add(e.ownerid);
        }
        delete eventlist;
        CalendarSorterNotFuture.sort(sortDate,Ownerid);
    }
}